clear all 
set more off, permanently

macro drop _all

********************************************************************************
* SET USERS, PATHS AND OTHER MACROS	
********************************************************************************

global user_dir "~/Dropbox/Work/Papers/MarkupCyclicality/2013_Endogenous_Firm_Competition/Final_Draft/Replication_files" 

global pathinit = "$user_dir/matlab_files/nr2020" // 

cd "$pathinit/markuprep-master/src/analysis" // must set cd because replication codes use relative directories



********************************************************************************
*  SET NR 2020 SETINGS
********************************************************************************
// project-wide Stata settings
noi run ../_config.do


// run programs as part of master script? (0 = no, 1 = yes)
global batch = 1


// choose standard error for IRF
/* global ci = "nose"  // no SE */
global ci = ""  // use asymptotic SE
// global ci = "bs rep(400)"  // use bootstrapped SE


// filter for extracting cyclical components
global fil = "hp"
/* global fil = "bk" */
/* global fil = "fd" */


// extract cyclical components for each filter
global oldfil = "$fil"
foreach fil in hp bk fd {
   global fil = "`fil'"
   noi run markups_cyc.do
}
global fil = "$oldfil"



********************************************************************************
*  RUN SUB-CODES svarmarkup_step_40 - extract_irfs
********************************************************************************

* IMPORTANT: use command 'run' instead of 'do', or else it may cause the program to fail.

* ------------------------------------------------------------------------------
* Same as svarmarkup.do but with step=40 in irf
noi di "Start code svarmarkup_step_40.do"
noi run "$pathinit/svarmarkup_step_40"
* ------------------------------------------------------------------------------


* ------------------------------------------------------------------------------
* Same as /markupcyc_replication/src/analysis/extract_irf.do
di "Start code extract_irf.do"

global linenumber_max = 49 // linenumber_max gets original results from NR 2020
run "$pathinit/extract_irfs"
* ------------------------------------------------------------------------------


********************************************************************************
*  EXPORT IRFs TO CSV
********************************************************************************

use "$pathinit/markuprep-master/output/oirf.dta", clear

foreach dataset in govt tfp {

	if "`dataset'" == "govt" local filename = "NR2020_gov"
	if "`dataset'" == "tfp"  local filename = "NR2020_tfp"

	preserve

	keep if shock == "`dataset'"

	export delimited using "$pathinit/`filename'.csv", replace

	restore
}

di "All set for matlab"
